{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Debugging LC3 Programs\n", "\n", "For debugging, consider using:\n", "\n", "* %pc ADDRESS\n", "* %step\n", "* %cont\n", "* %bp ADDRESS" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory dump:\n", "============================================================\n", " x3000: x5020\n", " x3001: x0405\n", " x3002: x1021\n", " x3003: x1022\n", " x3004: x1023\n", " x3005: x1024\n", " x3006: x1025\n", " x3007: xF025\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3008\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ ".ORIG x3000\n", "0101 000 000 1 00000 ; R0 <- R0 and 0\n", "0000 010 000000101 ; BR if zero to next, next statement\n", "0001 000 000 1 00001 ; R0 <- R0 + 1\n", "0001 000 000 1 00010 ; R0 <- R0 + 2\n", "0001 000 000 1 00011 ; R0 <- R0 + 3\n", "0001 000 000 1 00100 ; R0 <- R0 + 4\n", "0001 000 000 1 00101 ; R0 <- R0 + 5\n", "1111 0000 00100101 ; HALT\n", ".END" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 3\n", "Cycles: 21 (0.000010 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3008 \n" ] } ], "source": [ "%exe" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3006\n", "(1/6) BR x3006 (or 0) (x3006*: x0000)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Attempting to execute NOOP at x3005\n", "\n" ] } ], "source": [ "%step" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For help on the meta commands of LC3 in Jupyter, issue a question mark in a cell:\n", "\n", "```\n", "?\n", "```\n", "\n", "A window will show these contents:\n", "\n", "
\n",
    "This is the Calysto LC3 Jupyter kernel.\n",
    "\n",
    "LC3 Interactive Magic Directives: \n",
    "\n",
    " %bp [clear | SUSPENDHEX]           - show, clear, or set breakpoints\n",
    " %cont                              - continue running\n",
    " %dis [STARTHEX [STOPHEX]]          - dump memory as program\n",
    " %dump [STARTHEX [STOPHEX]]         - list memory in hex\n",
    " %exe                               - execute the program\n",
    " %mem HEXLOCATION HEXVALUE          - set memory\n",
    " %pc HEXVALUE                       - set PC\n",
    " %reg REG HEXVALUE                  - set register REG to HEXVALUE\n",
    " %regs                              - show registers\n",
    " %reset                             - reset LC3 to start state\n",
    " %step                              - execute the next instruction, increment PC\n",
    "\n",
    "HEX values begin with an 'x' and are composed of 4 0-F digits or letters.\n",
    "\n",
    "To get additional help on these items, use '%help %item'.\n",
    "\n",
    "To see additional magics, use %lsmagic, and put a question mark after a magic \n",
    "name.\n",
    "
" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " PC <= x3000\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3000\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%pc x3000" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3001\n", "(1/6) AND R0, R0, #0 [1] (x3001*: x5020)\n", " R0 <= x0000\n", " NZP <= (0, 1, 0)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3001\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3002\n", "(2/12) BRz x3003 (or 1) [2] (x3002*: x0401)\n", " PC <= x3003\n", " True - branching to x3003\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3003\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3004\n", "(3/18) ADD R0, R0, #2 [4] (x3004*: x1022)\n", " R0 <= x0002\n", " NZP <= (0, 0, 1)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3004\n", "N: 0 Z: 0 P: 1 \n", "R0: x0002 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3005\n", "(4/27) HALT [5] (x3005*: xF025)\n", " R7 <= x3005\n", " PC <= x048E\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0002 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3005 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x0490\n", "(6/42) PUTS (x0490*: xF022)\n", " R7 <= x0490\n", " PC <= x0456\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x0456\n", "N: 0 Z: 0 P: 1 \n", "R0: x04B0 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0490 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Trying a breakpoint.\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Breakpoints\n", "============================================================\n", " 1) x3004: xF025\n" ] } ], "source": [ "%bp x3004" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "...breakpoint hit at x3004\n", "============================================================\n", "Computation SUSPENDED\n", "============================================================\n", "Instructions: 3\n", "Cycles: 18 (0.000009 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3004\n", "N: 0 Z: 0 P: 1 \n", "R0: x0002 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%exe" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Stepping... => read, <= write, (Instructions/Cycles):\n", "============================================================\n", " PC <= x3005\n", "(4/27) HALT [5] (x3005*: xF025)\n", " R7 <= x3005\n", " PC <= x048E\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0002 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3005 \n" ] } ], "source": [ "%step" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "--- halting the LC-3 ---\n", "\n", "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 329\n", "Cycles: 2723 (0.001362 milliseconds)\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 1 P: 0 \n", "R0: x0000 R1: x7FFF R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0495 \n" ] } ], "source": [ "%cont" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto LC3", "language": "gas", "name": "calysto_lc3" }, "language_info": { "file_extension": ".asm", "mimetype": "text/x-gas", "name": "gas" } }, "nbformat": 4, "nbformat_minor": 0 }